// pages/myVoice/index.js
const app = getApp()
import { dateFormat } from '../../utils/dateFormat.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDeleteModal: false,
    voiceList: [],
    currentPage: 1,
    isDownLoading: true,
    loadingUp: false,
    hasContent: false,
    deleteVoiceId: ''
  },
  lookDetail(e){
    // console.log(e.currentTarget.dataset.voiceid)
    wx.navigateTo({
      url: '/pages/detail/index?voiceid=' + e.currentTarget.dataset.voiceid,
    })
  },
  longpressDelete(e){
    this.setData({
      showDeleteModal: true,
      deleteVoiceId: e.currentTarget.dataset.voiceid
    })
  },
  hideModal() {
    this.setData({
      showDeleteModal: false,
      deleteVoiceId: ''
    })
  },
  confirmDelete(){
    wx.showLoading({
      title: '删除中',
      mask: true,
    })
    wx.cloud.callFunction({
      name: 'deleteVoice',
      data: {
        voiceId: this.data.deleteVoiceId
      }
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          showDeleteModal: false
        })
        this.data.voiceList = this.data.voiceList.filter(item => {
          return item._id != this.data.deleteVoiceId
        })
        this.setData({
          deleteVoiceId: '',
          voiceList: this.data.voiceList
        })
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1000
        })
        app.sumCount = {}
      }
    }).catch(err => {
      console.log(err)
    })
  },
  getMyVoice(page){
    // console.log(app.user._openid)
    wx.cloud.callFunction({
      name: 'getVoice',
      data: {
        page,
        openId: app.user._openid
      }
    }).then(res => {
      if(res.result.length == 0){
        this.setData({
          isDownLoading: false,
          loadingUp: false
        })
        if(page == 1){
          this.setData({
            hasContent: true
          })
        }
        return
      }
      let list = res.result
      list.forEach((item, index) => {
        let content = item.voiceContent.replace(/<\/?.+?>/g, "")
        list[index].voiceContentAll = item.voiceContent
        list[index].voiceContent = content.substring(0, 30) + (content.length > 30 ? '...' : '') 
        list[index].submitDate = dateFormat(new Date(item.submitDate), 'yyyy-MM-dd hh:mm')
      })
      // console.log(list)
      this.setData({
        voiceList: this.data.voiceList.concat(list),
        loadingUp: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyVoice(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.isDownLoading) return
    this.setData({
      loadingUp: true
    })
    this.data.currentPage++
    this.getMyVoice(this.data.currentPage)
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})